//
//  AYSCategorySmallDeViewController.m
//  AiYingShiProject
//
//  Created by lanou on 15/11/3.
//  Copyright (c) 2015年 李晓理. All rights reserved.
//

#import "AYSCategorySmallDeViewController.h"
#import "AYSRequestManager.h"
#import "AYSSmallDetailView.h"
#import "AYSPlayerViewController.h"
#import "UMSocial.h"
#import "AYSFavouriteHandle.h"
#import "AYSDatabaseHandle.h"
#import "AYSPlayersViewController.h"
#import "Reachability.h"
@interface AYSCategorySmallDeViewController ()<UMSocialUIDelegate>
@property (nonatomic,strong) AYSSmallListModel *listModel;
//网络检测
@property (nonatomic, strong) Reachability *conn;
@end

@implementation AYSCategorySmallDeViewController
{
    UIAlertView *alertView;
    UIAlertView *alertView1;
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    UIColor *color = [UIColor whiteColor];
    NSDictionary *dict = [NSDictionary dictionaryWithObject:color forKey:UITextAttributeTextColor];
    self.navigationController.navigationBar.titleTextAttributes = dict;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithWhite:0.032 alpha:1.000];
    AYSSmallDetailView *smallDetialView = [[AYSSmallDetailView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) ];
    smallDetialView.loadImgView = [[TouchImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 2 - 50) taget:self action:@selector(getPlayVC)];
    [smallDetialView.shareButton addTarget:self action:@selector(shareButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [smallDetialView addSubview:smallDetialView.loadImgView];
    [smallDetialView.collectButton addTarget:self action:@selector(clickCollectButton:) forControlEvents:(UIControlEventTouchUpInside)];
    smallDetialView.navigationController = self.navigationController;
    [smallDetialView changeContentValueWithModel:self.model];
    [self.view addSubview:smallDetialView];
    
    //播放按钮的图片
    smallDetialView.playImageView = [[UIImageView alloc] initWithFrame:CGRectMake(smallDetialView.loadImgView.frame.size.width / 2 - 30, smallDetialView.loadImgView.frame.size.height / 2 - 50, 60, 60)];
    smallDetialView.playImageView.image = [UIImage imageNamed:@"播放2"];
    [smallDetialView.loadImgView addSubview:smallDetialView.playImageView];
    
    //网络检测
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil];
    self.conn = [Reachability reachabilityForInternetConnection];
    [self.conn startNotifier];

}

-(void)networkStateChange
{
    //    [self checkNetworkState];
    
}

-(void)dealloc
{
    [self.conn stopNotifier];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)clickCollectButton:(UIButton *)button
{
    BOOL isFavorite = [[AYSDatabaseHandle shareInstance] isFavoriteVideoWithID:_model.videoListID];
    if (YES == isFavorite)
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"该视频已经被收藏过" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alertView show];
        return;
    }
    
    // 操作数据库，收藏活动
    _model.isFavorite = YES;
    [[AYSDatabaseHandle shareInstance] insertNewVideo:_model];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    [alertView show];
    [self performSelector:@selector(p_removeAlertView:) withObject:alertView afterDelay:0.5];

}

- (void)p_removeAlertView:(UIAlertView *)alertView
{
    [alertView dismissWithClickedButtonIndex:0 animated:YES];
}
#pragma wark ============进入视频播放界面
- (void)getPlayVC
{
    //检测wifi状态
    Reachability *wifi = [Reachability reachabilityForLocalWiFi];
    //检测手机是否能上网(wifi\3G);
    Reachability *conn = [Reachability reachabilityForInternetConnection];
    //判断网络状态
    if ([wifi currentReachabilityStatus] != NotReachable)
    {
        //        NSLog(@"有wifi");
        
        alertView1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前处于WiFi状态" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        //设置弹窗是自动消失的
        [alertView1 show];
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(view1:) userInfo:nil repeats:NO];
    }else if ([conn currentReachabilityStatus] != NotReachable)
    {
        //        NSLog(@"使用手机自带网络进行上网");
        
        alertView1 = [[UIAlertView alloc] initWithTitle:@"提示"message:@"当前处于非WiFi状态" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView1 show];
    }else
    {
        //        NSLog(@"没有网络");
        
        alertView1 = [[UIAlertView alloc] initWithTitle:@"提示"message:@"网络无法连接" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil];
        [alertView1 show];
    }

//    AYSPlayerViewController *playVC = [[AYSPlayerViewController alloc] init];
//    playVC.videoSource = self.model.normalUrl;
//    [self.navigationController presentViewController:playVC animated:YES completion:nil];
    
}

#pragma mark----计时器
- (void)view1:(NSTimer *)timer
{
    [alertView1 dismissWithClickedButtonIndex:0 animated:YES];
    [self settingClick];
}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        [self settingClick];
    }
}

#pragma mark----设置图片的点击方法
- (void)settingClick
{
    NSURL *url = [NSURL URLWithString:self.model.normalUrl];
    AYSPlayersViewController *playerVC = [[AYSPlayersViewController alloc] initWithHTTPLiveStreamingMediaURL:url];
    playerVC.mediaTitle = self.model.title;
    [self presentViewController:playerVC animated:YES completion:nil];
    
}


// 分享
- (void)shareButton:(UIButton *)button
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"561b7a19e0f55a91c80015f2"
                                      shareText:@"你要分享的文字"
                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren, UMShareToSms,nil]
                                       delegate:self];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
